﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using ARSys.Connector;
using ARSys.Entity;
using Wicresoft.Logger;

namespace EVM.Implementation
{
    static class IAMHelper
    {
        private static readonly string ARServer = ConfigurationManager.AppSettings["ARServer"];
        private static readonly string ARUid = ConfigurationManager.AppSettings["ARUid"];
        private static readonly string ARPwd = ConfigurationManager.AppSettings["ARPwd"];

        public static IEnumerable<string> GetRoleList()
        {
            Log.Debug("IAM", "IAMHelper::GetRoleList was called");

            using (ARSysContext dc = new ARSysContext())
            {
                return dc.Groups.Where(p => p.Organization == "CIO").Select(p => p.Name).ToArray();
            }
        }

        public static IEnumerable<string> GetRolesByUser(string employeeId)
        {
            Log.Debug("IAM", "IAMHelper::GetRolesByUser was called");

            using (ARSysContext dc = new ARSysContext())
            {
                var sql = from role in dc.Groups
                          from people in dc.People
                          from pg in dc.PGAssociations
                          where people.PeopleId == pg.PeopleId
                          where role.GroupId == pg.GroupId
                          where people.EmployeeID == employeeId
                          select role.Name;

                return sql.ToArray();
            }
        }

        public static void AddRolesToUser(string employeeId, IEnumerable<string> roles)
        {
            Log.Debug("IAM", "IAMHelper::AddRolesToUser was called");

            using (ARSysContext dc = new ARSysContext())
            {
                using (ARSysProxy px = new ARSysProxy(ARServer, ARUid, ARPwd))
                {
                    using (ARTransaction tsc = px.BeginTransaction())
                    {
                        var ppl = dc.People.First(p => p.EmployeeID == employeeId);
                        var sql = from pg in dc.PGAssociations
                                  from people in dc.People
                                  where people.PeopleId == pg.PeopleId
                                  where people.EmployeeID == employeeId
                                  select pg;

                        foreach (var pg in sql.ToArray())
                        {
                            px.DeleteEntry(pg);
                        }

                        // ##set support group to NO!!
                        //ppl.SupportStaff = 1;
                        //px.SetEntry(ppl);

                        if (roles.Count() > 0)
                        {
                            foreach (var role in roles)
                            {
                                PGAssociation pg = new PGAssociation()
                                {
                                    GroupId = dc.Groups.First(p => p.Name == role).GroupId,
                                    PeopleId = ppl.PeopleId,
                                    Alias = ppl.CIL,
                                    FullName = ppl.FullName,
                                    Role = "2000",
                                };
                                px.CreateEntry(pg);
                            }

                            // ##set support group to YES!!
                            //ppl.SupportStaff = 0;
                            //px.SetEntry(ppl);
                        }

                        tsc.Commit();
                    }
                }
            }
        }

        /// <summary>
        /// ->UPI (string)
        /// ->Alias (string)
        /// ->Roles (string[])
        /// </summary>
        public static IEnumerable<dynamic> GetAllUsers()
        {
            Log.Debug("IAM", "IAMHelper::GetAllUsers was called");

            using (ARSysContext dc = new ARSysContext())
            {
                var sql = from pg in dc.PGAssociations
                          from people in dc.People
                          from role in dc.Groups
                          where people.PeopleId == pg.PeopleId
                          where role.GroupId == pg.GroupId
                          where dc.Groups.Count() != 0
                          select new
                          {
                              people.PeopleId,
                              people.EmployeeID,
                              people.CIL,
                          };

                foreach (var people in sql.Distinct().ToArray())
                {
                    dynamic user = new System.Dynamic.ExpandoObject();

                    user.EmployeeId = people.EmployeeID;
                    user.LoginName = people.CIL;
                    user.Roles = (from role in dc.Groups
                                  from pg in dc.PGAssociations
                                  where role.GroupId == pg.GroupId
                                  where pg.PeopleId == people.PeopleId
                                  select role.Name).ToArray();

                    if (!string.IsNullOrEmpty(user.EmployeeId))
                        yield return user;
                }
            }
        }
    }
}